package com.fengye.security.domain;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.*;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Set;

/**
 * @author fengyexjtu@126.com
 * @date 2022年04月13日 11:02 AM
 */

@Data
@With
@Builder
@Table(name = "mooc_users")
@Entity
@AllArgsConstructor
@NoArgsConstructor
public class User implements UserDetails, Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "username", length = 50, unique = true, nullable = false)
    private String username;

    @JsonIgnore
    @Column(name = "password_hash", length = 200, unique = true, nullable = false)
    private String password;

    @Column(name = "email", length = 80, unique = true, nullable = false)
    private String email;

    @Column(name = "mobile", length = 11, unique = true, nullable = false)
    private String mobile;

    @Column(name = "name", length = 50)
    private String name;

    @Column(name = "account_non_expired", nullable = false)
    private boolean accountNonExpired;
    @Column(name = "account_non_locked", nullable = false)
    private boolean accountNonLocked;
    @Column(name = "credentials_non_expired", nullable = false)
    private boolean credentialsNonExpired;
    @Column(name = "enabled", nullable = false)
    private boolean enabled;

    @ManyToMany
    @Fetch(value = FetchMode.JOIN) // join 查询,不用分次查询,提高性能
    @JoinTable(name = "mooc_users_roles",
            joinColumns = {@JoinColumn(name = "user_id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "role_id", referencedColumnName = "id")})
    private Set<Role> authorities;


}
